package br.usp.pcs.smarthouse.gwt.client;

import java.util.ArrayList;

import java.util.Date;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.CheckBox;
import java.util.LinkedHashMap;

//import br.usp.pcs.smarthouse.gwt.client.CleanningProduct;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SmartHouseUI implements EntryPoint {

	
	private DecoratedTabPanel tPanel = new DecoratedTabPanel();
	private VerticalPanel configPanel = new VerticalPanel();
	private HorizontalPanel homePanel = new HorizontalPanel();
	private VerticalPanel choresPanel = new VerticalPanel();
	private VerticalPanel closetPanel = new VerticalPanel();
	private VerticalPanel larderPanel = new VerticalPanel();
	private VerticalPanel docsPanel = new VerticalPanel();
               
        private LinkedHashMap docsTypeList = new LinkedHashMap();
        private int dtl = 0;
        private LinkedHashMap docsList = new LinkedHashMap();
        private int dl = 0;
        private LinkedHashMap prodList = new LinkedHashMap();
        private int pl = 0;

	private TextBox descriptionBox = new TextBox();
	private TextBox nameBox = new TextBox();
	private TextBox quantityBox = new TextBox();
	
	private Button clean = new Button("Limpar");
	
	private docsAtributtes doc = new docsAtributtes();
	
	FlexTable choresList = new FlexTable();
	
	//private ArrayList<CleanningProduct> products = new ArrayList<CleanningProduct>();
	private ArrayList<String> productsName = new ArrayList<String>();
	
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {            
	  
	  HTML welcomeText = new HTML("<h2>Bem vindo ao Smart House</h2>" +
	  							  "Navegue pelas abas para achar sua opcao");	  
          
          homePanel.add(welcomeText);                    
	  
	  configPanel.add(buildInsertLoc());
	  configPanel.add(buildListLoc());
	  

	  choresPanel.add(buildInsertCleanningProduct());
	  choresPanel.add(buildListCleanningProduct());
	  choresPanel.add(buildInsertNewChores());
	  choresPanel.add(buildListChores());
            
      //docsPanel.add(buildDocsToPay());
      docsPanel.add(buildAllDocs());
      docsPanel.add(buildInsertDoc());
      docsPanel.add(buildInsertDocType());
      docsPanel.add(buildListDocsType());

	  closetPanel.add(buildInsertNewClothes());
	  closetPanel.add(buildListClothes());

	  larderPanel.add(buildListFood());	  
	  larderPanel.add(buildListExpiredFood());
	  larderPanel.add(buildInsertFood());
	  larderPanel.add(buildInsertFoodType());	  
	  
	  
	  clean.addClickListener(new ClickListener(){
			 public void onClick(Widget sender){
				 Date atual = new Date();
				 Date prox = new Date();
				 prox.setDate(atual.getDate()+14);
				 choresList.setText(1, 4, DateTimeFormat.getShortDateFormat().format(atual));
				 choresList.setText(1, 5, DateTimeFormat.getShortDateFormat().format(prox));
				 choresList.setText(1, 6, "nao");
			 }
	  });
	  
	  
	  
	  tPanel.setAnimationEnabled(true);
	  tPanel.add(homePanel, "HOME");
	  tPanel.add(configPanel, "CONFIGURACOES");
	  tPanel.add(choresPanel, "CHORES");
	  tPanel.add(larderPanel, "LARDER");
	  tPanel.add(docsPanel,"DOCUMENTS");
	  tPanel.add(closetPanel, "CLOSET");
	  
	  tPanel.selectTab(0);
	  tPanel.setWidth("100%");
	  
	  RootPanel.get().add(tPanel);
          
  }
  
  public Widget buildListClothes(){
	  FlexTable locations = new FlexTable();
	  locations.setCellSpacing(8);
	  locations.setHTML(0, 0, "<b>RFID Tag</b>");
	  locations.setHTML(0, 1, "<b>Tipo da Roupa</b>");
	  locations.setHTML(0, 2, "<b>Material</b>");
	  locations.setHTML(0, 3, "<b>Cor principal</b>");
	  locations.setHTML(0, 4, "<b>Descricao</b>");
	  locations.setHTML(0, 5, "<b>Localizacao</b>");
	  
	  locations.setText(1, 0, "12345");
	  locations.setText(1, 1, "Vestido");
	  locations.setText(1, 2, "Seda");
	  locations.setText(1, 3, "vermelho");
	  locations.setText(1, 4, "vestido curto");
	  locations.setText(1, 5, "2 gaveta da direita");
	  
	  locations.setText(2, 0, "7489595");
	  locations.setText(2, 1, "bulsa");
	  locations.setText(2, 2, "algodao");
	  locations.setText(2, 3, "branca");
	  locations.setText(2, 4, "blusa com listra preta");
	  locations.setText(2, 5, "1 gaveta do centro");
	  
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 5, HasHorizontalAlignment.ALIGN_CENTER);
	  
	  
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Listar roupas");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(locations);
	  
	  return dPanel;
  }
  
  public Widget buildInsertNewClothes(){
	  Grid options = new Grid(6,2);
	  options.setCellSpacing(8);
	  options.setText(0, 0, "RFID Tag:");
	  options.setWidget(0, 1, new TextBox());
	  options.setText(1, 0, "Descricao:");
	  options.setWidget(1, 1, new TextBox());
	  options.setText(2, 0, "Tipo da Roupa:");
	  options.setWidget(2, 1, new ListBox());
	  options.setText(3, 0, "Material:");
	  options.setWidget(3, 1, new ListBox());
	  options.setText(4, 0, "Cor principal:");
	  options.setWidget(4, 1, new ListBox());
	  options.setWidget(5, 1, new Button("Adicionar"));
	  options.getCellFormatter().setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Inserir nova roupa");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public Widget buildInsertNewChores(){
	  Grid options = new Grid(5,2);
	  options.setCellSpacing(8);
	  options.setText(0, 0, "Tempo de duracao da tarefa (em min):");
	  options.setWidget(0, 1, new TextBox());
	  options.setText(1, 0, "Local / objeto que sera limpo:");
	  options.setWidget(1, 1, new ListBox());
	  options.setText(2, 0, "Produto de limpeza:");
	  options.setWidget(2, 1, new ListBox());
	  options.setText(3, 0, "Periodicidade da limpeza (em semanas):");
	  options.setWidget(3, 1, new TextBox());
	  options.setWidget(4, 1, new Button("Adicionar"));
	  options.getCellFormatter().setHorizontalAlignment(4, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Inserir nova tarefa");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public Widget buildListChores(){
	    
	  choresList.setCellSpacing(8);
	  choresList.setHTML(0, 0, "<b>Local/obejto da limpeza</b>");
	  choresList.setHTML(0, 1, "<b>Produto de limpeza</b>");
	  choresList.setHTML(0, 2, "<b>Quantidade</b>");
	  choresList.setHTML(0, 3, "<b>Tempo de duracao da tarefa (em min)</b>");
	  choresList.setHTML(0, 4, "<b>Data da ultima limpeza</b>");
	  choresList.setHTML(0, 5, "<b>Data da proxima limpeza</b>");
	  choresList.setHTML(0, 6, "<b>Pendente</b>");
	  
	  choresList.setText(1, 0, "quarto");
	  choresList.setText(1, 1, "veja");
	  choresList.setText(1, 2, "4");
	  choresList.setText(1, 3, "30");
	  choresList.setText(1, 6, "sim");
	  choresList.setWidget(1, 7, clean);
	  
	  
	  
	  
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 5, HasHorizontalAlignment.ALIGN_CENTER);
	  choresList.getFlexCellFormatter().setHorizontalAlignment(0, 6, HasHorizontalAlignment.ALIGN_CENTER);
	  
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Listar tarefas");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(choresList);
	  
	  return dPanel;
  }
  
  public Widget buildListCleanningProduct(){
	  Button add = new Button("Adicionar");
	  Button rmvOne = new Button("Remover 1");
	  Button rmvAll = new Button("Remover todos");
	   
	  int numDet = 2;
	
	  Label num = new Label();
	  num.setText("1");
	  
	  final FlexTable locations = new FlexTable();
	  FlexCellFormatter cellFormatter = locations.getFlexCellFormatter();
	  locations.setCellSpacing(8);
	  locations.setHTML(0, 0, "<b>Descricao</b>");
	  locations.setHTML(0, 1, "<b>Nome</b>");
	  locations.setHTML(0, 2, "<b>Quantidade</b>");

	  locations.setText(1, 0, "Veja Multi-Uso");
	  locations.setText(1, 1, "veja");
	  locations.setWidget(1, 2, num);
	  locations.setWidget(1, 3, add);
	  locations.setWidget(1, 4, rmvOne);
	  locations.setWidget(1, 5, rmvAll);
	  locations.setText(2, 0, "detergente");
	  locations.setText(2, 1, "detergente");
	  locations.setText(2, 2, Integer.toString(numDet));
	  
	  add.addClickListener(new ClickListener(){
			 public void onClick(Widget sender){
				 Label changeWidget = (Label)locations.getWidget(1, 2);
				 changeWidget.setText(Integer.toString(Integer.parseInt(changeWidget.getText())+1));
			 }
	  });
	  
	  rmvOne.addClickListener(new ClickListener(){
			 public void onClick(Widget sender){
				 Label changeWidget = (Label)locations.getWidget(1, 2);
				 changeWidget.setText(Integer.toString(Integer.parseInt(changeWidget.getText())-1));
			 }
	  });
	  
	  cellFormatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	  cellFormatter.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
	  cellFormatter.setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
	  cellFormatter.setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
	  cellFormatter.setHorizontalAlignment(2, 2, HasHorizontalAlignment.ALIGN_CENTER);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Listar produtos de limpeza");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(locations);
	  
	  return dPanel;
  }
  
  public Widget buildInsertCleanningProduct(){
	  Button add = new Button("Adicionar"); 
	  
	  Grid options = new Grid(4,2);
	  options.setCellSpacing(8);
	  options.setText(0, 0, "Descricao do produto:");
	  options.setWidget(0, 1, descriptionBox);
	  options.setText(1, 0, "Nome do produto:");
	  options.setWidget(1, 1, nameBox);
	  options.setText(2, 0, "Quantidade:");
	  options.setWidget(2, 1, quantityBox);
	  options.setWidget(3, 1, add);
	  options.getCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  add.addClickListener(new ClickListener(){
		 public void onClick(Widget sender){
			 addCP();
		 }
	  });

	  DisclosurePanel dPanel = new DisclosurePanel("Inserir produto de limpeza");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public void addCP(){
	  final String description = descriptionBox.getText();
	  final String name = nameBox.getText();
	  final int quantity = Integer.parseInt(quantityBox.getText());
	  
	  if (productsName.contains(name))
		  return;
	  
	  productsName.add(name);
//	  CleanningProduct temp = new CleanningProduct(description, name, quantity);
//	  products.add(temp);
	  
	  descriptionBox.setText("");
	  nameBox.setText("");
	  quantityBox.setText("");
  }
  
  public Widget buildInsertLoc(){
	  Grid options = new Grid(2,2);
	  options.setCellSpacing(8);
	  options.setHTML(0, 0, "Localizacao");
	  options.setWidget(0, 1, new TextBox());
	  options.setWidget(1, 1, new Button("Adicionar"));
	  options.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Inserir localizacao");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public Widget buildInsertFoodType(){
	  Grid options = new Grid(3,2);
	  options.setCellSpacing(8);
	  options.setHTML(0, 0, "Tipo de Alimento");
	  final TextBox tipo = new TextBox();
	  options.setWidget(0, 1, tipo);
	  //options.setHTML(1, 0, "Categoria");
	  //options.setWidget(1, 1, new ListBox());
	  Button b = new Button("Adicionar");
	  options.setWidget(2, 1, b);
	  b.addClickListener(new ClickListener(){
		  public void onClick(Widget sender){
			  produtos p = new produtos();
			  p.setNome(tipo.getText());
			  prodList.put(pl++, p);
		  }
	  });
	  options.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Inserir Tipo de Alimento");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public Widget buildInsertFood(){
	  Grid options = new Grid(8,2);
	  options.setCellSpacing(8);
	  options.setHTML(0, 0, "<b>Informacoes do Alimento</b>");
	  options.setHTML(1, 0, "Tipo do Alimento");
	  options.setWidget(1, 1, new ListBox());
	  options.setHTML(2, 0, "Data de Validade (em meses)");
	  options.setWidget(2, 1, new TextBox());
	  options.setHTML(3, 0, "Unidade");
	  options.setWidget(3, 1, new ListBox());
	  options.setHTML(4, 0, "Quantidade");
	  options.setWidget(4, 1, new TextBox());
	  options.setWidget(7, 1, new Button("Adicionar"));
	  options.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Inserir Alimento");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(options);
	  
	  return dPanel;
  }
  
  public Widget buildListLoc(){
	    
	  FlexTable locations = new FlexTable();
	  locations.setCellSpacing(8);
	  locations.setHTML(0, 0, "<b>Locais</b>");
	  locations.setHTML(1, 0, "Cozinha");
	  locations.setHTML(2, 0, "Sala");
	  
	  
	  locations.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	  
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Listar localizacoes");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(locations);
	  
	  return dPanel;
  }
  
  public Widget buildListFood(){
	  
	  FlexTable foods = new FlexTable();
	  foods.setCellSpacing(8);
	  foods.setHTML(0, 0, "<b>Alimentos</b>");
	  foods.setHTML(1, 0, "<b>Descricao do Alimento</b>");
	  foods.setHTML(1, 1, "<b>Data de Validade</b>");
	  foods.setHTML(1, 2, "<b>Comprado Dia</b>");
	  foods.setHTML(1, 3, "<b>Aberto Dia</b>");
	  foods.setHTML(1, 4, "<b>Localizacao</b>");
	  foods.setHTML(1, 5, "<b>Aberto?</b>");
	  
	  foods.setHTML(2, 0, "Tomate");
	  foods.setWidget(2, 5, new CheckBox());
	  
	  foods.setHTML(3, 0, "Chocolate");
	  foods.setWidget(3, 5, new CheckBox());
	  
	  foods.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	  
	  DisclosurePanel dPanel = new DisclosurePanel("Lista Alimentos");
	  dPanel.setAnimationEnabled(true);
	  dPanel.setContent(foods);
	  
	  return dPanel;
  }

    private Widget buildAllDocs() {
    
    	
    FlexTable docs = new FlexTable();
	docs.setCellSpacing(8);
	docs.setHTML(0, 0, "<b>Tipo documento</b>");
	docs.setHTML(0, 1, "<b>Descricao</b>");
	docs.setHTML(0, 2, "<b>Valor a pagar</b>");
	docs.setHTML(0, 3, "<b>Dia do vencimento</b>");
	docs.setHTML(0, 4, "<b>Dia em que foi pago</b>");
	docs.setHTML(0, 5, "<b>Localizacao</b>");
    docs.setHTML(0, 6, "<b>Data Retirado</b>");
	  
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 5, HasHorizontalAlignment.ALIGN_CENTER);
    docs.getFlexCellFormatter().setHorizontalAlignment(0, 6, HasHorizontalAlignment.ALIGN_CENTER);
        
        for(int i =0; i<docsList.size(); i=i+1){
        	Button payed = new Button("PAGO");
            
            doc = (docsAtributtes) docsList.get(i);
            docs.setHTML(i+1, 0, doc.getType());
            docs.setHTML(i+1, 1, doc.getDescription());
            docs.setHTML(i+1, 2, doc.getValue());
            docs.setHTML(i+1, 3, doc.getPaymentDay());
            docs.setHTML(i+1, 4, doc.getPayedDay());
            docs.setHTML(i+1, 5, doc.getLocation());
            docs.setHTML(i+1, 6, doc.getTakenDay());            
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 0, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 1, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 2, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 3, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 4, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 5, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 6, HasHorizontalAlignment.ALIGN_CENTER);
            docs.setWidget(i+1, 7, payed);
            payed.addClickListener(new ClickListener(){
            	public void onClick(Widget sender){
            		
            		doc.setPayedDay(DateTimeFormat.getShortDateFormat().format(new Date()));
            	}
            });
            docsList.put(i, doc);
        }

	DisclosurePanel dPanel = new DisclosurePanel("Lista dos Documentos");
	dPanel.setAnimationEnabled(true);
	dPanel.setContent(docs);
	  
	return dPanel;
    }   
    
    private Widget buildDocsToPay() {
        
        FlexTable docs = new FlexTable();
	docs.setCellSpacing(8);
	docs.setHTML(0, 0, "<b>Tipo documento</b>");
	docs.setHTML(0, 1, "<b>Descricao</b>");
	docs.setHTML(0, 2, "<b>Valor a pagar</b>");
	docs.setHTML(0, 3, "<b>Dia do vencimento</b>");	
	docs.setHTML(0, 4, "<b>Localizacao</b>");
        docs.setHTML(0, 5, "<b>Data Retirado</b>");
	  
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 5, HasHorizontalAlignment.ALIGN_CENTER);        
	  
	DisclosurePanel dPanel = new DisclosurePanel("Documentos a Pagar");
	dPanel.setAnimationEnabled(true);
	dPanel.setContent(docs);
	  
	return dPanel;
    }
    
    private Widget buildInsertDoc(){
	Grid options = new Grid(8,4);
	options.setCellSpacing(8);
	options.setText(0, 0, "Tipo do documento:");
        
        final ListBox type = new ListBox();
        for(int i =0; i<docsTypeList.size(); i=i+1){
            docsType t = new docsType();
            t = (docsType) docsTypeList.get(i);
            type.addItem(t.getDescription());
            type.setVisibleItemCount(i);
        }        
        
	options.setWidget(0, 1, type);
	options.setText(1, 0, "Descricao:");
        final TextBox description = new TextBox();
	options.setWidget(1, 1, description);
	options.setText(2, 0, "Localizacao:");
        final ListBox location = new ListBox();
	options.setWidget(2, 1, location);
        options.setText(3, 0, "Valor a Pagar:");        
        final TextBox value = new TextBox();        
	options.setWidget(3, 1, value);
        options.setText(4, 0, "Dia vencimento:");
        final TextBox date = new TextBox();        
	options.setWidget(4, 1, date);        
        Button b = new Button("Adicionar");
	options.setWidget(5, 1, b);
        b.addClickListener(new ClickListener() {
            public void onClick(Widget arg0) {                
                if(description.getText().equals("")){
                    Window.alert("Descrição não pode ser nula!");                    
                }
                else if((type.getItemText(type.getSelectedIndex()).equals("A Pagar")) &&
                        (value.getText().equals("")||date.getText().equals(""))){
                    Window.alert("Valor ou Data nula!");
                }
                else{
                    docsAtributtes doc = new docsAtributtes();
                    doc.setType(type.getValue(type.getSelectedIndex()));
                    doc.setDescription(description.getText());
                    //doc.setLocation(location.getValue(location.getSelectedIndex()));
                    doc.setValue(value.getText());
                    doc.setPaymentDay(date.getText());
                    docsList.put(dl++, doc);                    
                    docsPanel.clear();
                    //docsPanel.add(buildDocsToPay());
                    docsPanel.add(buildAllDocs());
                    docsPanel.add(buildInsertDoc());
                    docsPanel.add(buildInsertDocType());
                    docsPanel.add(buildListDocsType());
                    
                }                
            }
        });
        
	options.getCellFormatter().setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	DisclosurePanel dPanel = new DisclosurePanel("Cadastrar documento");
	dPanel.setAnimationEnabled(true);
	dPanel.setContent(options);
	  
	return dPanel;
    }
    
    private Widget buildInsertDocType() {        
        Grid options = new Grid(4,2);
	options.setCellSpacing(8);
	options.setText(0, 0, "Nome/Descrição:");
        final TextBox t = new TextBox();
	options.setWidget(0, 1, t);
	options.setText(1, 0, "Tipo:");
        final ListBox lb = new ListBox();
        lb.addItem("A Pagar");
        lb.addItem("Geral");
        lb.setVisibleItemCount(2);
	options.setWidget(1, 1, lb);
        Button b = new Button("Adicionar");                            
	options.setWidget(2, 1, b);
        b.addClickListener(new ClickListener() {
            public void onClick(Widget arg0) {                
                if(t.getText().equals("")){
                    Window.alert("Descrição não pode ser nula!");                    
                }
                else{
                    docsType type = new docsType();
                    type.setDescription(t.getText());
                    type.setSubType(lb.getItemText(lb.getSelectedIndex()));
                    docsTypeList.put(dtl++, type);                    
                    docsPanel.clear();
                    //docsPanel.add(buildDocsToPay());
                    docsPanel.add(buildAllDocs());
                    docsPanel.add(buildInsertDoc());
                    docsPanel.add(buildInsertDocType());
                    docsPanel.add(buildListDocsType());
                    Window.alert("Novo tipo de documento inserido!");
                }                
            }
        });
        
	options.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	  
	DisclosurePanel dPanel = new DisclosurePanel("Cadastrar tipo documento");
	dPanel.setAnimationEnabled(true);
	dPanel.setContent(options);
	  
	return dPanel;
    }    

    private Widget buildListDocsType() {
        FlexTable docs = new FlexTable();
	docs.setCellSpacing(8);	
	docs.setHTML(0, 0, "<b>Nome/Descrição</b>");
	docs.setHTML(0, 1, "<b>Tipo</b>");	
	  
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	docs.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);     
                        
        for(int i =0; i<docsTypeList.size(); i=i+1){
            docsType type = new docsType();
            type = (docsType) docsTypeList.get(i);
            docs.setHTML(i+1, 0, type.getDescription());
            docs.setHTML(i+1, 1, type.getSubType());
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 0, HasHorizontalAlignment.ALIGN_CENTER);
            docs.getFlexCellFormatter().setHorizontalAlignment(i+1, 1, HasHorizontalAlignment.ALIGN_CENTER);
        }
	  
	DisclosurePanel dPanel = new DisclosurePanel("Tipos de Documentos");
	dPanel.setAnimationEnabled(true);
	dPanel.setContent(docs);
	  
	return dPanel;
    } 
    
    public Widget buildListExpiredFood(){
	  
	  FlexTable xFoods = new FlexTable();
	  
	 xFoods.setCellSpacing(8);
	 
	 xFoods.setHTML(0, 0, "<b>Alimentos Contra Indicados Para o Consumo</b>");
	 
	 xFoods.setHTML(1, 0, "<b>Descricao</b>");
	 xFoods.setHTML(1, 1, "<b>Data De Vencimento</b>");
	 xFoods.setHTML(1, 2, "<b>Localizacao</b>");
	 	 
	 xFoods.setHTML(2, 0, "Frango Congelado");
	 xFoods.setHTML(3, 0, "Laranja");
	 xFoods.setHTML(4, 0, "Iogurte");
	 
	 xFoods.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	 
	 DisclosurePanel dPanel = new DisclosurePanel("Lista de Alimentos Vencidos");
	 dPanel.setAnimationEnabled(true);
	 dPanel.setContent(xFoods);
	 
	 return dPanel;
  }     
}
